home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevherc.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  12KB  |  477 lines

  1. /* Copyright (C) 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevherc.c */
  20. /* IBM PC-compatible Hercules Graphics display driver for Ghostscript */
  21. /* using direct access to frame buffer */
  22.  
  23. #define FB_RASTER 90
  24. #define SCREEN_HEIGHT 350
  25. #define SCREEN_ASPECT_RATIO (54.0/35.0)
  26. #define VIDEO_MODE 0x07
  27. #define regen 0xb0000000L
  28.  
  29. #define interrupt            /* patch ANSI incompatibility */
  30. #include "dos_.h"
  31. typedef union REGS registers;
  32. #include "gx.h"
  33. #include "gsmatrix.h"            /* for gxdevice.h */
  34. #include "gxbitmap.h"
  35. #include "gxdevice.h"
  36.  
  37. /* Dimensions of screen */
  38. #define screen_size_x (FB_RASTER * 8)
  39. #define screen_size_y SCREEN_HEIGHT
  40. /* Other display parameters */
  41. #define raster_x FB_RASTER
  42. #define aspect_ratio SCREEN_ASPECT_RATIO
  43. #define graphics_video_mode VIDEO_MODE
  44.  
  45. /* Procedures */
  46.  
  47.     /* See gxdevice.h for the definitions of the procedures. */
  48.  
  49. dev_proc_open_device(herc_open);
  50. dev_proc_close_device(herc_close);
  51. dev_proc_fill_rectangle(herc_fill_rectangle);
  52. dev_proc_copy_mono(herc_copy_mono);
  53. dev_proc_copy_color(herc_copy_color);
  54.  
  55. /* The device descriptor */
  56. private gx_device_procs herc_procs = {
  57.     herc_open,
  58.     gx_default_get_initial_matrix,
  59.     gx_default_sync_output,
  60.     gx_default_output_page,
  61.     herc_close,
  62.     gx_default_map_rgb_color,
  63.     gx_default_map_color_rgb,
  64.     herc_fill_rectangle,
  65.     gx_default_tile_rectangle,
  66.     herc_copy_mono,
  67.     herc_copy_color
  68. };
  69.  
  70. gx_device gs_herc_device = {
  71.     sizeof(gx_device),
  72.     &herc_procs,
  73.     "herc",
  74.     screen_size_x, screen_size_y,
  75.     /* The following parameters map an appropriate fraction of */
  76.     /* the screen to a full-page coordinate space. */
  77.     /* This may or may not be what is desired! */
  78.       (screen_size_y * aspect_ratio) / PAGE_HEIGHT_INCHES,    /* x dpi */
  79.       screen_size_y / PAGE_HEIGHT_INCHES,        /* y dpi */
  80.     no_margins,
  81.     dci_black_and_white,
  82.     0            /* not opened yet */
  83. };
  84.  
  85.  
  86. /* Forward declarations */
  87. private int herc_get_mode(P0());
  88. private void herc_set_mode(P1(int));
  89.  
  90. /* Save the HERC mode */
  91. private int herc_save_mode = -1;
  92.  
  93. /* Reinitialize the herc for text mode */
  94. int
  95. herc_close(gx_device *dev)
  96. {    if ( herc_save_mode >= 0 ) herc_set_mode(herc_save_mode);
  97.     return 0;
  98. }
  99.  
  100. /* ------ Internal routines ------ */
  101.  
  102. /* Read the device mode */
  103. private int
  104. herc_get_mode(void)
  105. {    registers regs;
  106.     regs.h.ah = 0xf;
  107.     int86(0x10, ®s, ®s);
  108.     return regs.h.al;
  109. }
  110.  
  111. /* Set the device mode */
  112. private void
  113. herc_set_mode(int mode)
  114. {    registers regs;
  115.     regs.h.ah = 0;
  116.     regs.h.al = mode;
  117.     int86(0x10, ®s, ®s);
  118. }
  119.  
  120.  
  121. /****************************************************************/
  122. /* Hercules graphics card functions                */
  123. /*                                */
  124. /* -- Taken from Jan/Feb 1988 issue of Micro Cornucopia #39    */
  125. /*                                */
  126. /* --rewritten for MSC 5.1 on 02/18/91 by Phillip Conrad    */
  127. /****************************************************************/
  128.  
  129.  
  130. static const char paramg[12] = {0x35, 0x2d, 0x2e, 0x07, 0x5b, 0x02,
  131.                   0x57, 0x57, 0x02, 0x03, 0x00, 0x00};
  132. /* (Never used)
  133. static const char paramt[12] = {0x61, 0x50, 0x52, 0x0f, 0x19, 0x06,
  134.                   0x19, 0x19, 0x02, 0x0d, 0x0b, 0x0c};
  135. */
  136.  
  137. /* Type and macro for frame buffer pointers. */
  138. /*** Intimately tied to the 80x86 (x<2) addressing architecture. ***/
  139. typedef byte far *fb_ptr; 
  140. #  define mk_fb_ptr(x, y)\
  141.     (fb_ptr)((regen) + ((0x2000 * ((y) % 4) + (90 * ((y) >> 2))) + ((int)(x) >> 3)))
  142.  
  143.  
  144. /* Structure for operation parameters. */
  145. /* Note that this structure is known to assembly code. */
  146. /* Not all parameters are used for every operation. */
  147. typedef struct rop_params_s {
  148.     fb_ptr dest;            /* pointer to frame buffer */
  149.     int draster;            /* raster of frame buffer */
  150.     const byte far *src;        /* pointer to source data */
  151.     int sraster;            /* source raster */
  152.     int width;            /* width in bytes */
  153.     int height;            /* height in scan lines */
  154.     int shift;            /* amount to right shift source */
  155.     int invert;            /* 0 or -1 to invert source */
  156.     int data;            /* data for fill */
  157.     int x_pos;        /*>>added--2/24/91 */
  158.     int y_pos;
  159. } rop_params;
  160.  
  161. /* Define the device port and register numbers, and the regen map base */
  162. #define seq_addr 0x3b4        /* changed for HERC card (6845 ports)*/
  163. #define graph_mode 0x3b8
  164. #define graph_stat 0x3ba
  165. #define graph_config 0x3bf
  166.  
  167. #ifndef regen
  168. #define regen 0xa0000000L
  169. #endif
  170.  
  171.  
  172. /* Initialize the display for Hercules graphics mode */
  173. int
  174. herc_open(gx_device *dev)
  175. {    int i;
  176.     if ( herc_save_mode < 0 ) herc_save_mode = herc_get_mode();
  177. /*    herc_set_mode(graphics_video_mode);  */
  178.     outportb(graph_config,3);
  179.     for(i=0;i<sizeof(paramg);i++)
  180.     {
  181.         outport2(seq_addr,i,paramg[i]); 
  182.         
  183.     }
  184.     outportb(graph_mode,0x0a);    /* set page 0 */
  185.     for(i=0;i<0x3FFFL;i++)    /* clear the screen */
  186.         {
  187.         int far *loc = (int far *)( regen  +(2L*i));
  188.         *loc = 0;
  189.         }
  190.  
  191.     return 0;
  192. }
  193.  
  194. /* Macro for testing bit-inclusion */
  195. #define bit_included_in(x,y) !((x)&~(y))
  196.  
  197. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  198. /* Color = gx_no_color_index means transparent (no effect on the image). */
  199. int
  200. herc_copy_mono(gx_device *dev,
  201.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  202.   int x, int y, int w, int h, gx_color_index izero, gx_color_index ione)
  203. {    rop_params params;
  204. #define czero (int)izero
  205. #define cone (int)ione
  206.     int dleft, sleft, count;
  207.     int invert, zmask, omask;
  208.     byte mask, rmask;
  209.  
  210.     if ( cone == czero )        /* vacuous case */
  211.         return herc_fill_rectangle(dev, x, y, w, h, izero);
  212.  
  213.     /* clip */
  214.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  215.     params.dest = mk_fb_ptr(x, y);
  216.     params.draster = raster_x;
  217.     params.src = base + (sourcex >> 3);
  218.     params.sraster = raster;
  219.     params.height = h;
  220.     params.shift = (x - sourcex) & 7;
  221.     params.y_pos = y;
  222.     params.x_pos = x;
  223.     params.width = w;
  224.  
  225.     if(czero > cone) params.invert = -1;
  226.  
  227.     /* Macros for writing partial bytes. */
  228.     /* bits has already been inverted by xor'ing with invert. */
  229.  
  230. #define write_byte_masked(ptr, bits, mask)\
  231.   *ptr = ((bits | ~mask | zmask) & (*ptr | (bits & mask & omask)))
  232.  
  233. #define write_byte(ptr, bits)\
  234.   *ptr = ((bits | zmask) & (*ptr | (bits & omask)))
  235.  
  236.     invert = (czero == 1 || cone == 0 ? -1 : 0); 
  237. /*    invert = (czero == 1 || cone == 1 ? -1 : 0); */
  238.     zmask = (czero == 0 || cone == 0 ? 0 : -1);
  239.     omask = (czero == 1 || cone == 1 ? -1 : 0);
  240.  
  241. #undef czero
  242. #undef cone
  243.  
  244.     /* Actually copy the bits. */
  245.  
  246.     sleft = 8 - (sourcex & 7);
  247.     dleft = 8 - (x & 7);
  248.     mask = 0xff >> (8 - dleft);
  249.     count = w;
  250.     if ( w < dleft )
  251.         mask -= mask >> w,
  252.         rmask = 0;
  253.     else
  254.         rmask = 0xff00 >> ((w - dleft) & 7);
  255.  
  256.     if (sleft == dleft)        /* optimize the aligned case */
  257.     {
  258.         w -= dleft;
  259.         while ( --h >= 0 )
  260.         {
  261.             register const byte *bptr = params.src;
  262.             register byte *optr = mk_fb_ptr(params.x_pos,params.y_pos);
  263.             register int bits = *bptr ^ invert;    /* first partial byte */
  264.  
  265.             count = w;
  266.                         
  267.             write_byte_masked(optr, bits, mask);
  268.             
  269.             /* Do full bytes. */
  270.  
  271.             while ((count -= 8) >= 0)
  272.             {
  273.                 bits = *++bptr ^ invert;
  274.                 params.x_pos += 8;
  275.                 optr = mk_fb_ptr(params.x_pos,params.y_pos);
  276.                 write_byte(optr, bits);
  277.             }
  278.             /* Do last byte */
  279.             
  280.             if (count > -8)
  281.             {
  282.                 bits = *++bptr ^ invert;
  283.                 params.x_pos += 8;
  284.                 optr = mk_fb_ptr(params.x_pos,params.y_pos);
  285.                 write_byte_masked(optr, bits, rmask);
  286.             }
  287. /*            dest += BPL; */
  288.             params.y_pos++;
  289.             params.x_pos = x;
  290.             params.src += raster;
  291.         }
  292.     }
  293.     else
  294.     {
  295.         int skew = (sleft - dleft) & 7;
  296.         int cskew = 8 - skew;
  297.         
  298.         while (--h >= 0)
  299.         {
  300.             const byte *bptr = params.src;
  301.             byte *optr = mk_fb_ptr(params.x_pos,params.y_pos);
  302.             register int bits;
  303.  
  304.             count = w;
  305.                         
  306.             /* Do the first partial byte */
  307.             
  308.             if (sleft >= dleft)
  309.             {
  310.                 bits = *bptr >> skew;
  311.             }    
  312.             else /* ( sleft < dleft ) */
  313.             {
  314.                 bits = *bptr++ << cskew;
  315.                 if (count > sleft)
  316.                     bits += *bptr >> skew;
  317.             }
  318.             bits ^= invert;
  319.             write_byte_masked(optr, bits, mask);
  320.             count -= dleft;
  321.             params.x_pos += 8;
  322.             optr = mk_fb_ptr(params.x_pos,params.y_pos);
  323.             
  324.             /* Do full bytes. */
  325.             
  326.             while ( count >= 8 )
  327.             {
  328.                 bits = *bptr++ << cskew;
  329.                 bits += *bptr >> skew;
  330.                 bits ^= invert;
  331.                 write_byte(optr, bits);
  332.                 count -= 8;
  333.                 params.x_pos += 8;
  334.                 optr = mk_fb_ptr(params.x_pos,params.y_pos);
  335.             }
  336.             
  337.             /* Do last byte */
  338.             
  339.             if (count > 0)
  340.             {
  341.                 bits = *bptr++ << cskew;
  342.                  if (count > skew)
  343.                     bits += *bptr >> skew;
  344.                 bits ^= invert;
  345.                 write_byte_masked(optr, bits, rmask);
  346.             }
  347. /*            dest += BPL;
  348.             line += raster;
  349. */
  350.             params.y_pos++;
  351.             params.x_pos = x;
  352.             params.src += raster;
  353.         }
  354.     }
  355.     return 0;
  356. }
  357.  
  358. /* Copy a color pixelmap.  This is just like a bitmap, */
  359. int
  360. herc_copy_color(gx_device *dev,
  361.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  362.   int x, int y, int w, int h)
  363. {    return herc_copy_mono(dev, base, sourcex, raster, id,
  364.         x, y, w, h,(gx_color_index)0, (gx_color_index)1);
  365. }
  366.  
  367. #  define mk_fb_yptr(x, y)\
  368.     (fb_ptr)((regen) + ((0x2000 * ((y) % 4) + (90 * ((y) >> 2))) + x))
  369.  
  370. /* Fill a rectangle. */
  371. int
  372. herc_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  373.   gx_color_index color)
  374. {    rop_params params;
  375.  
  376.     int x2, y2, xlen;
  377.     byte led, red, d;
  378.     byte far *ptr;
  379.     int xloc;
  380.  
  381.     fit_fill(dev, x, y, w, h);
  382.  
  383.     params.dest = mk_fb_ptr(x, y);
  384.     params.y_pos = y;
  385.     params.x_pos = x;
  386.  
  387.     x2 = x + w - 1;
  388.     y2 = y + h - 1;
  389.  
  390.     xlen = (x2 >> 3) - (x >> 3) - 1;
  391.     led = 0xff >> (x & 7);
  392.     red = 0xff << (7 - (x2 & 7));
  393.  
  394.     ptr =  mk_fb_ptr(x,y);
  395.  
  396.     if (color)
  397.     {
  398.         /* here to set pixels */
  399.         
  400.         if (xlen == -1)
  401.         {
  402.             /* special for rectangles that fit in a byte */
  403.             
  404.             d = led & red;
  405.             for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  406.             {
  407.                 *ptr |= d;
  408.                 params.y_pos++;
  409.             }
  410.             return 0;
  411.         }
  412.         
  413.         /* normal fill */
  414.         
  415.         xloc = params.x_pos >> 3;
  416.         for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  417.         {    register int x_count = xlen;
  418.             register byte far *p = ptr;
  419.             *p |= led;
  420. /*             params.x_pos += 8; */
  421.             xloc++;
  422.              p = mk_fb_yptr(xloc,params.y_pos);
  423.             while ( x_count-- ) {
  424.                  *p = 0xff;
  425. /*                 params.x_pos += 8; */
  426.                 xloc++;
  427.                  p = mk_fb_yptr(xloc,params.y_pos);
  428.                 }
  429.             *p |= red;
  430. /*            params.x_pos = x; */
  431.             xloc = params.x_pos >> 3;
  432.             params.y_pos++;
  433.         }
  434.     }
  435.  
  436.     /* here to clear pixels */
  437.  
  438.     led = ~led;
  439.     red = ~red;
  440.  
  441.     if (xlen == -1)
  442.     {
  443.         /* special for rectangles that fit in a byte */
  444.         
  445.         d = led | red;
  446.         for(; h >= 0; h--, ptr  = mk_fb_ptr(x,params.y_pos))
  447.             {
  448.             *ptr &= d;
  449.             params.y_pos++;
  450.             }
  451.         return 0;
  452.     }
  453.  
  454.     /* normal fill */
  455.         
  456.     xloc = x >> 3;
  457.     for(; h >= 0; h--, ptr = mk_fb_ptr(x,params.y_pos))
  458.     {    register int x_count = xlen;
  459.         register byte far *p = ptr;
  460.         *p &= led;
  461. /*         params.x_pos += 8; */
  462.         xloc++;
  463.          p = mk_fb_yptr(xloc,params.y_pos);
  464.         while ( x_count-- ) {
  465.              *p = 0x00;
  466. /*             params.x_pos += 8; */
  467.             xloc++;
  468.              p = mk_fb_yptr(xloc,params.y_pos);
  469.             }
  470.         *p &= red;
  471. /*        params.x_pos = x; */
  472.         xloc = params.x_pos >> 3;
  473.         params.y_pos++;
  474.     }
  475.     return 0;
  476. }
  477.